//
//  SettingViewController+ClearCache.m
//  JKFuYinProject
//
//  Created by jiukuyinyue on 2017/9/30.
//  Copyright © 2017年 LC. All rights reserved.
//

#import "SettingViewController+ClearCache.h"
#import "LCCatCacheSize.h"
#import "LCDeleteFile.h"

@implementation SettingViewController (ClearCache)

- (NSString *)readCacheSize {
    if ([LCCatCacheSize readCacheSize] < 2) {
        return [NSString stringWithFormat:@"0.0M"];
    }else{
        return [NSString stringWithFormat:@"%.1fM",  [LCCatCacheSize readCacheSize]];
    }

}
///清除数据库里的纪录缓存
- (void)clearDB {
    [[YYCache cacheWithName:@"LCChannelListsCache"] removeAllObjects];
    [[YYCache cacheWithName:@"LCHomeChannelName"]   removeAllObjects];
    [[YYCache cacheWithName:@"LCCache"]             removeAllObjects];
    [LSZSpecialPackaging deleteAllCache];

}

- (void)clearCache {

    //清除缓存
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"提示"
                                                                   message:@"清除后，缓存歌曲将无法离线收听，确定要清除吗？"
                                                preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
        [SVProgressHUD show];
        [SVProgressHUD dismissWithDelay:1];
        [LCDeleteFile deleteFileWithISHasSuffix:NO];
        [self clearDB];

        //清除历史纪录
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"BIBLETEXT"];
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"TENSONGS"];
                                                                  _LCPlayer.selectedData = nil;
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"TENVIDEOS"];
                                                                  _LCPlayer.selectedVideoData = nil;
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"DOWNLOADONGS"];
                                                                  _LCPlayer.downloadData = nil;
        //网页缓存
        NSSet *websiteDataTypes =[WKWebsiteDataStore allWebsiteDataTypes];

        NSDate *dateFrom = [NSDate dateWithTimeIntervalSince1970:0];

        [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:websiteDataTypes modifiedSince:dateFrom completionHandler:^{

        }];
        [LSZSpecialPackaging deleteAllCache];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.view animated:YES];
            hud.mode = MBProgressHUDModeCustomView;
            hud.label.text = @"清除成功!";
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            self.huanCunLabel.text = [self readCacheSize];
        });
    });
    }];
    NSString *cancelStr = @"取消";
    UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:cancelStr style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:defaultAction];
    
    [alert addAction:cancelAction];
    [self presentViewController:alert animated:YES completion:nil];
}

@end
